//
//  AddFriendSearchView.m
//  BaseAPP
//
//  Created by Evisu on 16/12/20.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "AddFriendSearchView.h"
#import "SearchListCell.h"

@interface AddFriendSearchView ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation AddFriendSearchView

- (UISearchBar *)searchBar {
    
    if (!_searchBar) {
        
        _searchBar = [[DMSearchBar alloc] init];
        
    }
    
    return _searchBar;
}

- (UITableView *)tableView {

    if (!_tableView) {
        
        _tableView = [[UITableView alloc] init];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width_sd, 20)];
        _tableView.backgroundColor = [UIColor clearColor];
    }
    
    return _tableView;
}

- (instancetype)init {

    if (self = [super init]) {
        
        self.results = [NSArray array];
        
        [self initViews];
        [self layoutViews];
        
        [self.searchBar becomeFirstResponder];
    }
    
    return self;
}

- (void)initViews {

    //搜索栏
    [self addSubview:self.searchBar];
    
    //搜索结果列表
    [self addSubview:self.tableView];
}


- (void)layoutViews {

    self.searchBar.sd_layout
    .topSpaceToView(self, 0)
    .leftEqualToView(self)
    .rightEqualToView(self)
    .heightIs(KSEARCHBAR_HEIGHT);
    
    self.tableView.sd_layout
    .topSpaceToView(self.searchBar, 0)
    .leftEqualToView(self)
    .rightEqualToView(self)
    .bottomEqualToView(self);
}


#pragma mark - tableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.results.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *reuseId = @"searchList";
    
    SearchListCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    
    if (!cell) {
        
        cell = [[SearchListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
    }
    
    cell.searchStr = self.results[indexPath.row];
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if (section == 1) {
        
        return @"手机联系人";
    }
    
    return @"";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 64.0;
}

#pragma mark - tableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    DMLog(@"搜索：%@", _results.firstObject);
}

- (void)setResults:(NSArray *)results {
    
    _results = results;
    
    [self.tableView reloadData];
}



- (void)dealloc {

    [self.searchBar resignFirstResponder];
}

@end
